package com.ita.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ita.dao.UserDao;
import com.ita.dao.impl.UserDaoImpl;
import com.ita.pojo.User;
import com.ita.util.NumberUtil;

@WebServlet("/info")
public class ShowInfoServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private UserDao userDao;

	public ShowInfoServlet() {
		userDao = new UserDaoImpl();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		userDao = new UserDaoImpl();
		List<User> users = userDao.findAll();
		if (users != null) {
			StringBuilder result = new StringBuilder();
			result.append("<h1>userinfo</h1>");
			result.append("<meta charset=\"utf-8\"></meta>");
			result.append("<table style='border:1px solid #000'><tr><td>username</td><td>salary</td><td>birth</td><td>sex</td><td>avatar</td></tr>");
			for (User user : users) {
				System.out.println(user.getUsername());
				result.append("<tr><td>"
						+ user.getUsername()
						+ "</td><td>"
						+ user.getSalary()
						+ "</td><td>"
						+ NumberUtil.toString(user.getBirth())
						+ "</td><td>"
						+ user.getSex()
						+ "</td><td><image width=30px height=30px src='showAva?name="
						+ user.getUsername() + "'</td></tr>");
			}
			result.append("</table>");
			PrintWriter writer = response.getWriter();
			writer.write(result.toString());
			writer.close();
		}
	}
}
